package cn.wonderland.resthome.service.impl;

import cn.wonderland.basic.util.BussinessException;
import cn.wonderland.resthome.domain.Nurse;
import cn.wonderland.resthome.mapper.NurseMapper;
import cn.wonderland.resthome.service.INurseService;
import cn.wonderland.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static cn.wonderland.resthome.util.GenerateId.generateId;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wonderland
 * @since 2023-05-01
 */
@Service
public class NurseServiceImpl extends BaseServiceImpl<Nurse> implements INurseService {
    @Autowired
    private NurseMapper nurseMapper;

    @Override
    public void add(Nurse nurse) {
        // 生成8位护工编号
        String nurseNumber = generateId();
        // 查找当前护工编号是否存在
        Integer haveNumber =  nurseMapper.findByNurseNumber(nurseNumber);
        // 判断编号是否已存在
        if (haveNumber != null || haveNumber != 0) {
            nurseNumber = generateId(); // 重新生成编号
        }
        // 查找现在有多少个正常状态的护工 ---- 最多存在 **正常**状态下的10个护工
        List<Nurse> count = nurseMapper.getNurseCount();
        Integer state = nurse.getState();
        if (count.size() >= 10 && state == 26){
            throw new BussinessException("很抱歉！最多存在10个正常状态员工！");
        }
        // 设置护工编号
        nurse.setNurseNumber(nurseNumber);
        // 设置护工创建时间
        nurse.setCreateTime(new Date());
        // 添加到数据库
        nurseMapper.add(nurse);
    }

    @Override
    public List<Nurse> getNurseCount() {
        return nurseMapper.getNurseCount();
    }


}
